package com.myjava.managesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myjava.managesystem.common.BaseContext;
import com.myjava.managesystem.entity.AddressBook;
import com.myjava.managesystem.entity.OrderDetail;
import com.myjava.managesystem.entity.Orders;
import com.myjava.managesystem.entity.ShoppingCart;
import com.myjava.managesystem.mapper.OrdersMapper;
import com.myjava.managesystem.service.AddressBookService;
import com.myjava.managesystem.service.OrderDetailService;
import com.myjava.managesystem.service.OrdersService;
import com.myjava.managesystem.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    AddressBookService addressBookService;

    @Override
    public void submitOrder(Orders order) {
        Long userId = BaseContext.getCurrentId();
        order.setUserId(userId);
        order.setOrderTime(LocalDateTime.now());
        LambdaQueryWrapper<AddressBook> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(AddressBook::getId, order.getAddressBookId());
        AddressBook addressBook = addressBookService.getOne(wrapper1);
        order.setAddress(addressBook.getDetail());
        order.setPhone(addressBook.getPhone());
        order.setConsignee(addressBook.getConsignee());
        order.setCheckoutTime(LocalDateTime.now());
        order.setAmount(BigDecimal.valueOf(0));
        this.save(order);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(wrapper);
        Set<Long> ids = new HashSet<>();
        BigDecimal amount = new BigDecimal(0);
        for (ShoppingCart s : shoppingCarts) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(order.getId());
            orderDetail.setAmount(s.getAmount());
            orderDetail.setNumber(s.getNumber());
            orderDetail.setDishFlavor(s.getDishFlavor());
            orderDetail.setDishId(s.getDishId());
            orderDetail.setSetmealId(s.getSetmealId());
            orderDetail.setName(s.getName());
            orderDetail.setImage(s.getImage());
            amount = amount.add(s.getAmount().multiply(BigDecimal.valueOf(s.getNumber())));
            orderDetailService.save(orderDetail);
            ids.add(s.getId());
        }
        order.setAmount(amount);
        this.updateById(order);
        shoppingCartService.removeByIds(ids);
    }
}
